package com.xhwl.centiotdata.handler.vehicle.impl;

import com.xhwl.centiotdata.handler.vehicle.IVehicleSync;
import com.xhwl.centiotdata.service.cent.IVehicleCentService;
import com.xhwl.centiotdata.suppliersystem.vehicle.nantou.NanTouHandler;
import com.xhwl.common.dto.cent.vehicle.feign.CreateVehicleAccessRecordForInNanTouFeignDTO;
import com.xhwl.common.dto.cent.vehicle.feign.CreateVehicleAccessRecordForOutNanTouFeignDTO;
import com.xhwl.common.dto.cent.vehicle.http.nantou.AccessRecordForInNanTouDTO;
import com.xhwl.common.dto.cent.vehicle.http.nantou.AccessRecordForOutNanTouDTO;
import com.xhwl.common.dto.cent.vehicle.http.nantou.query.QueryAccessRecordNanTouDTO;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleIntegrationConfigBO;
import com.xhwl.common.pojo.cent.vehicle.converter.feign.AccessRecordSecondFeignServiceConverter;
import com.xhwl.common.utils.vehicle.AboutDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.xhwl.centiotdata.pojo.constant.vehicle.VehicleConstant.*;

@Slf4j
@Service
public class NanTouSync implements IVehicleSync {
    @Autowired
    private NanTouHandler nanTouHandler;

    @Autowired
    private IVehicleCentService vehicleCentService;

    @Autowired
    private AccessRecordSecondFeignServiceConverter accessRecordSecondFeignServiceConverter;

    @Override
    public void sync(VehicleIntegrationConfigBO vehicleIntegrationConfigBO) {
        Integer projectId = vehicleIntegrationConfigBO.getVehicleSystemInfoBO().getProjectId();
        String parkId = vehicleIntegrationConfigBO.getVehicleParkingInfoBO().getParkId();

        Date end = AboutDateUtil.theSameHourOfDate(new Date(), vehicleTimeZone);
        Date start = AboutDateUtil.dateMinusHour(end, 1);

        QueryAccessRecordNanTouDTO queryAccessRecordNanTouDTO = new QueryAccessRecordNanTouDTO(parkId, AboutDateUtil.dateToString(start, vehicleTimeZone, vehicleDateTimeFormat), AboutDateUtil.dateToString(end, vehicleTimeZone, vehicleDateTimeFormat));

        List<AccessRecordForInNanTouDTO> accessRecordForInNanTouDTOList = nanTouHandler.queryAccessRecordForIn(vehicleIntegrationConfigBO, queryAccessRecordNanTouDTO);
        if (!accessRecordForInNanTouDTOList.isEmpty()) {
            log.info("access record for in is not empty, sync in nan-tou start!");
            List<CreateVehicleAccessRecordForInNanTouFeignDTO> createVehicleAccessRecordForInNanTouFeignDTOList = accessRecordSecondFeignServiceConverter.toCreateVehicleAccessRecordForInNanTouFeignDTOList(accessRecordForInNanTouDTOList, projectId);
            vehicleCentService.addAccessRecordsForIn(createVehicleAccessRecordForInNanTouFeignDTOList);
            log.info("access record for in is not empty, sync in nan-tou end!");
        } else {
            log.info("no access record for in to sync in nan-tou!");
        }

        List<AccessRecordForOutNanTouDTO> accessRecordForOutNanTouDTOList = nanTouHandler.queryAccessRecordForOut(vehicleIntegrationConfigBO, queryAccessRecordNanTouDTO);
        if (!accessRecordForOutNanTouDTOList.isEmpty()) {
            log.info("access record for out is not empty, sync in nan-tou start!");
            List<CreateVehicleAccessRecordForOutNanTouFeignDTO> createVehicleAccessRecordForOutNanTouFeignDTOList = accessRecordSecondFeignServiceConverter.toCreateVehicleAccessRecordForOutNanTouFeignDTOList(accessRecordForOutNanTouDTOList, projectId);
            vehicleCentService.addAccessRecordsForOut(createVehicleAccessRecordForOutNanTouFeignDTOList);
            log.info("access record for out is not empty, sync in nan-tou end!");
        } else {
            log.info("no access record for out to sync in nan-tou!");
        }
    }
}
